package com.itly.controller;

import com.itly.entity.Message;
import com.itly.service.MessageService;
import com.itly.util.ApiResult;
import com.itly.util.ApiResultHandler;
import com.itly.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class MessageController {
    private Logger logger = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    private MessageService messageService;

    @PostMapping("message")
    public ApiResult addMessage(@RequestBody Message message) {
        int i = messageService.addMessage(message);
        if (i == 1) {
            logger.info("留言添加成功");
            return ApiResultHandler.buildApiResult(200, "添加留言成功", i);
        } else {
            logger.info("留言添加失败",message);
            return ApiResultHandler.buildApiResult(400, "添加留言失败", i);
        }
    }

    @GetMapping("messages/{page}/{size}")
    public ApiResult messages(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        PageBean<Message> messagePageBean = messageService.MessageByPage(page, size);
        logger.info("分页查找成功");
        return ApiResultHandler.buildApiResult(200, "分页查找成功", messagePageBean);
    }

    @GetMapping("/message/{id}")
    public ApiResult selectById(@PathVariable("id") Integer id) {
        Message message = messageService.selectById(id);
        logger.info("通过id查询留言消息");
        return ApiResultHandler.buildApiResult(200, "查询消息", message);
    }

}
